package com.lcz.mytest.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.lcz.mytest.R;
import com.lcz.mytest.activity.MainActivity;
import com.lcz.mytest.db.UserService;
import com.lcz.mytest.domain.User;

import java.util.List;

/**
 * 收藏功能适配器
 * Created by Administrator on 2016/5/16.
 */
public class CollectAdapter extends BaseAdapter {
    private List<User> list;
    private Context context;
    private TextView collect_tip;

    public CollectAdapter(List<User> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public User getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        MyHolder holder;
        View view = convertView;
        if (view == null) {
            holder = new MyHolder();
            view = View.inflate(context, R.layout.activity_collect_item, null);
            holder.code = (TextView) view.findViewById(R.id.stockcode);
            collect_tip = (TextView) view.findViewById(R.id.collect_tip);
            view.setTag(holder);
        } else {
            holder = (MyHolder) view.getTag();
        }
        holder.code.setText((position + 1) + "." + list.get(position).getStockCode() + "-" + list.get(position).getDataType());
        final User user = new User();
        user.setName(MainActivity.USERNAME);//用户名
        user.setStockCode(list.get(position).getStockCode());//个股代码
        user.setDataType(list.get(position).getDataType());//数据类型
        TextView delete = (TextView) view.findViewById(R.id.delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(context)
                        .setTitle("提示")
                        .setMessage("确认删除？")
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                                        int which) {
                                        //将股票代码从相应用户的收藏表中删除
                                        UserService service = new UserService(context);
                                        service.deleteCollect(user);
                                        if (service.deleteCollect(user)) {
                                            Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                                            //获取收藏列表
                                            List<User> codeList = service.collectList(MainActivity.USERNAME);
                                            if (codeList==null){
                                                collect_tip.setVisibility(View.VISIBLE);
                                            }else{
                                                collect_tip.setVisibility(View.GONE);
                                                list = codeList;
                                                notifyDataSetChanged();
                                            }
                                        } else {
                                            Toast.makeText(context, "系统出错了，请稍后删除", Toast.LENGTH_SHORT).show();
                                        }
                                    }

                                }).setNegativeButton("取消", null).show();
            }
        });
        return view;
    }

    public void upDataList(List<User> listData) {
        this.list = listData;
    }

    static class MyHolder {
        TextView code;
    }
}
